<template>
  <div class="user-list-container">
    <div class="filter-container">
      <el-input
          v-model="searchQuery"
          placeholder="请输入用户名或邮箱"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter="handleSearch"
      />
      <el-select v-model="roleValue" placeholder="用户角色" clearable style="width: 150px" class="filter-item">
        <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" class="filter-item" @click="handleSearch">搜索</el-button>
      <el-button type="success" class="filter-item" @click="handleCreate">新增用户</el-button>
    </div>

    <el-table
        v-loading="loading"
        :data="userList"
        border
        style="width: 100%"
    >
      <el-table-column prop="id" label="ID" width="80"/>
      <el-table-column prop="username" label="用户名" min-width="120"/>
      <el-table-column prop="email" label="邮箱" width="180"/>
      <el-table-column prop="phone" label="电话" width="180"/>
      <el-table-column prop="role" label="角色" width="100">
        <template #default="{row}">
          <el-tag :type="row.role === 'admin' ? 'danger' : 'info'">{{ row.role }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{row}">
          <el-switch
              v-model="row.status"
              :active-value="true"
              :inactive-value="false"
              @change="handleStatusChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
        <template #default="{row}">
          <span>{{ formatTime(new Date(row.createTime)) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="180">
        <template #default="{row}">
          <span>{{ formatTime(new Date(row.updateTime)) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="lastLogin" label="最后登录" width="180"/>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{row}">
          <el-button size="small" @click="handleEdit(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import {onMounted, ref} from 'vue'
import {findAllUser} from "@/api/user";

export default {
  name: 'UserList',
  setup() {
    const loading = ref(false)
    const searchQuery = ref('')
    const roleValue = ref('')
    const currentPage = ref(1)
    const pageSize = ref(10)
    const total = ref(50)

    const roleOptions = [
      {value: '管理员', label: '管理员'},
      {value: '编辑', label: '编辑'},
      {value: '普通用户', label: '普通用户'}
    ]

    const userList = ref([])

    const handleSearch = () => {
      loading.value = true
      findAllUser().then(res => {
        loading.value = false
        userList.value = res.data
      })
    }

    const handleCreate = () => {
      console.log('创建新用户')
    }

    const handleEdit = (row) => {
      console.log('编辑用户', row)
    }

    const handleDelete = (row) => {
      console.log('删除用户', row)
    }

    const handleStatusChange = (row) => {
      console.log('状态变更', row)
    }

    const handleSizeChange = (val) => {
      pageSize.value = val
      // 重新加载数据
      handleSearch()
    }

    const handleCurrentChange = (val) => {
      currentPage.value = val
      // 重新加载数据
      handleSearch()
    }

    // 格式化时间
    const formatTime = (date) => {
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      let hours = date.getHours();
      let minutes = date.getMinutes();
      let seconds = date.getSeconds();

      month = month < 10 ? "0" + month : month;
      day = day < 10 ? "0" + day : day;
      hours = hours < 10 ? "0" + hours : hours;
      minutes = minutes < 10 ? "0" + minutes : minutes;
      seconds = seconds < 10 ? "0" + seconds : seconds;

      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }

    onMounted(() => {
      handleSearch()
    })

    return {
      loading,
      searchQuery,
      roleValue,
      roleOptions,
      userList,
      currentPage,
      pageSize,
      total,
      handleSearch,
      handleCreate,
      handleEdit,
      handleDelete,
      handleStatusChange,
      handleSizeChange,
      handleCurrentChange,
      formatTime,
    }
  }
}
</script>

<style scoped>
.user-list-container {
  padding: 20px;
}

.filter-container {
  margin-bottom: 20px;
}

.filter-item {
  margin-right: 10px;
  margin-bottom: 10px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>
